﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace KetBanBonPhuong
{
    /// <summary>
    /// Summary description for Post
    /// </summary>
    public class Post
    {
        private int post_id;

        public int Post_id
        {
            get
            {
                return post_id;
            }
            set
            {
                post_id = value;
            }
        }

        private int group_id;

        public int Group_id
        {
            get
            {
                return group_id;
            }
            set
            {
                group_id = value;
            }
        }

        private string title;

        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
            }
        }

        private string text;

        public string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
            }
        }

        private DateTime created_date;

        public DateTime Created_date
        {
            get
            {
                return created_date;
            }
            set
            {
                created_date = value;
            }
        }

        private DateTime modified_date;

        public DateTime Modified_date
        {
            get
            {
                return modified_date;
            }
            set
            {
                modified_date = value;
            }
        }

        private bool is_shown;

        public bool Is_shown
        {
            get
            {
                return is_shown;
            }
            set
            {
                is_shown = value;
            }
        }


        public Post()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Post(SqlDataReader sdr)
        {
            if (sdr != null && !sdr.IsClosed)
            {
                post_id = sdr.GetInt32(0);
                if (sdr.IsDBNull(1))
                    group_id = sdr.GetInt32(1);
                if (sdr.IsDBNull(2))
                    title = sdr.GetString(2);
                if (sdr.IsDBNull(3))
                    text = sdr.GetString(3);
                if (sdr.IsDBNull(4))
                    created_date = sdr.GetDateTime(4);
                if (sdr.IsDBNull(5))
                    modified_date = sdr.GetDateTime(5);
                if (sdr.IsDBNull(6))
                    is_shown = sdr.GetBoolean(6);
            }
        }

        public static DataTable GetAll()
        {
            DBHelp dbh = new DBHelp();
            DataTable dt = new DataTable();
            string cmd = "Post_GetAll";
            dt = dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
            return dt;
        }

        public static DataTable GetAllPostOfGroup(int GroupID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@group_id", GroupID);
            String cmd = "select * from tbl_post where group_id = @group_id";
            DataTable dt = dbh.ExecuteDataTable(cmd, CommandType.Text);
            return dt;
        }

        //Ham lay ve ID nhan vao
        public static Post GetInfo(int ID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@post_id", ID);
            Post post = new Post();
            string cmd = "Post_GetInfo";
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if (sdr.Read())
            {
                post.Post_id = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    post.Group_id = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    post.Title = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    post.Text = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    post.Created_date = sdr.GetDateTime(4);
                if (!sdr.IsDBNull(5))
                    post.Modified_date = sdr.GetDateTime(5);
                if (!sdr.IsDBNull(6))
                    post.Is_shown = sdr.GetBoolean(6);
            }
            return post;
        }

        //Them post
        public static void Insert(Post post)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@group_id", post.Group_id);
            dbh.AddParameter("@title", post.Title);
            dbh.AddParameter("@text", post.Text);
            //dbh.AddParameter("@text", post.Text, SqlDbType.NText, ParameterDirection.Input);
            dbh.AddParameter("@created_date", post.Created_date);
            dbh.AddParameter("@modified_date", post.Modified_date);
            dbh.AddParameter("@is_shown", post.Is_shown);
            string cmd = "Post_Insert";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        //Sua post
        public static void Update(Post post)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@post_id", post.Post_id);
            dbh.AddParameter("@group_id", post.Group_id);
            dbh.AddParameter("@title", post.Title);
            dbh.AddParameter("@text", post.Text);
            dbh.AddParameter("@created_date", post.Created_date);
            dbh.AddParameter("@modified_date", post.Modified_date);
            dbh.AddParameter("@is_shown", post.Is_shown);
            string cmd = "Post_Update";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        //Xoa post
        public static void Delete(int ID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@post_id", ID);
            string cmd = "Post_Delete";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        // can thiet them sau
    }
}